﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Agro.Library;
using Ninject;
using Agro.Services;
using SN.Core;
using SN.Data;
using Agro.Models;
using System.Web.Security;
using System.IO;

namespace Agro.Web.Areas.SystemManage.Controllers
{
    public class NoticeController : BaseController
    {

        [Inject]
        public INoticeService NoticeService { get; set; }
        //
        // GET: /SystemManage/Notice/

        public ActionResult NoticeList()
        {
            ViewBag.CurrUser = CurrUser.UserInfo.User_ID;
            return View();
        }

        public ActionResult GetNoticeList(string id, QueryBase query)
        {
            WhereClip wclip = WhereClip.All;
            var dt = NoticeService.GetNoticeList(query, wclip);
            return JqGrid(dt, query);
        }

        public ActionResult NoticeDetail(string id)
        {
            T_Notice Notice;
            if (string.IsNullOrEmpty(id))
            {
                Notice = new T_Notice();
                ViewBag.NoticeType = "add";

            }
            else
            {
                Notice = NoticeService.GetNoticeDetail(id);
                ViewBag.NoticeType = "read";
            }
            ViewBag.auth = Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
            ViewBag.GUID = Guid.NewGuid().ToString().Replace("-", "");
            //不传送Message.AttachmentFile,因为是二进制文件，可能很大，影响页面打开速度。
            Notice.Notice_AttachmentFile = null;
            return View(Notice);
        }

        public ActionResult NoticeDetailPage(string id)
        {
            T_Notice Notice;
            if (string.IsNullOrEmpty(id))
            {
                Notice = new T_Notice();
                ViewBag.NoticeType = "add";
            }
            else
            {
                Notice = NoticeService.GetNoticeDetail(id);
                ViewBag.NoticeType = "read";
            }

            T_User user = NoticeService.GetEntity<T_User>(T_User._.User_ID == Notice.Notice_CreateUser);
            T_Organization org = NoticeService.GetEntity<T_Organization>(T_Organization._.Organization_ID == user.User_OrganizationID);
            ViewBag.PublishOrg = org.Organization_Name;
            ViewBag.auth = Request.Cookies[FormsAuthentication.FormsCookieName] == null ? string.Empty : Request.Cookies[FormsAuthentication.FormsCookieName].Value;
            ViewBag.GUID = Guid.NewGuid().ToString().Replace("-", "");
            //不传送Message.AttachmentFile,因为是二进制文件，可能很大，影响页面打开速度。
            Notice.Notice_AttachmentFile = null;
            return View(Notice);
        }

        public ActionResult Delete_Notice(string ids)
        {
            string Msg = string.Empty;
            NoticeService.DeleteNotice(ids, out Msg);
            return Json(Msg);

        }


        [HttpPost]
        [ValidateInput(false)]//此句用于处理 “从客户端中检测到有潜在危险的 Request.Form 值”
        public ActionResult NoticeSave(HttpPostedFileBase FileData, string GUID, string Notice_ID, FormCollection collection)
        {
            string Msg = string.Empty;
            byte[] bytes = null;
            string fileName = string.Empty;


            ///////////////////////////////////////////////处理附件
            if (!string.IsNullOrEmpty(GUID))
            {

                fileName = Path.GetFileName(FileData.FileName);
                int FileLen;
                FileLen = FileData.ContentLength;
                bytes = new byte[FileLen];
                FileData.InputStream.Read(bytes, 0, FileLen);

            }


            T_Notice Notice = new T_Notice();

            if (Convert.ToInt32(Notice_ID) > 0)
            {
                Notice = NoticeService.GetEntity<T_Notice>(T_Notice._.Notice_ID == Notice_ID);

            }
            else
            {
                Notice.Notice_AttachmentName = fileName;
                Notice.Notice_AttachmentFile = bytes;
            }

            UpdateModel(Notice, collection);
            string msg = string.Empty;
            NoticeService.SaveNotice(Notice, out msg, CurrUser);

            if (string.IsNullOrEmpty(Msg))
            {
                return SNJson(Notice.Notice_ID, Msg);
            }
            else
            {
                return SNJson("", Msg);
            }

        }

        public void DownLoadFile(string id)
        {
            string msg = string.Empty;

            T_Notice Notice = new T_Notice();
            Response.Clear();
            if (string.IsNullOrWhiteSpace(id))
            {
                msg = "请选择要下载的文件！";
                Response.Write("<script>alert('提示:" + msg + "');</script>");
                Response.End();
                return;
            }
            //必须是收信人或发信人才能下载附件
            Notice = NoticeService.GetEntity<T_Notice>(T_Notice._.Notice_ID == id);

            //处理下载
            Response.Clear();
            string filePath = Notice.Notice_AttachmentName;
            if (Notice.Notice_AttachmentFile == null)
            {
                msg = "所要下载的文件不存在！";
                Response.Write("<script>alert('提示:" + msg + "');</script>");
                Response.End();
                return;
            }
            Response.ContentType = "application/octet-stream";
            //通知浏览器下载文件而不是打开
            //Content-Disposition参数：
            //attachment --- 作为附件下载    
            //inline --- 在线打开
            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filePath, System.Text.Encoding.UTF8));

            Response.BinaryWrite(Notice.Notice_AttachmentFile);
            Response.Flush();
            Response.End();


        }

    }
}
